package com.she.iiicoursecenter.view.tab3;

import java.util.List;

import ken.app.helper.Helper;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.she.iiicoursecenter.R;
import com.she.iiicoursecenter.activity.tab1_infomation.Tab1_CourseSingle;
import com.she.iiicoursecenter.activity.tab3_reservation.Tab3_MainActivity;
import com.she.iiicoursecenter.adapter.Tab3_Adapter;
import com.she.iiicoursecenter.bean.CourseSingleBundleBean;
import com.she.iiicoursecenter.bean.ViewChageDataBean;
import com.she.iiicoursecenter.event.base.Event;
import com.she.iiicoursecenter.event.connect.bean.GetMakeBean;
import com.she.iiicoursecenter.event.connect.bean.HideMakeExpireBean;
import com.she.iiicoursecenter.event.connect.event.GetMakeEvent;
import com.she.iiicoursecenter.event.connect.event.HideMakeExpireEvent;
import com.she.iiicoursecenter.event.iface.IEventCallback;
import com.she.iiicoursecenter.eventmanager.KenEventManager;
import com.she.iiicoursecenter.view.template.CustomizedView;

public class Tab3_MainView extends CustomizedView implements OnClickListener {

	private ListView mListView;
	private Tab3_Adapter mAdapter;
	private List<GetMakeBean> data;
	private ImageView mTab3Button;
	private Handler handler = new Handler(){
		@Override
		public void dispatchMessage(Message msg) {
			if (msg.obj != null) {
				Toast.makeText(getContext(), (String)msg.obj, Toast.LENGTH_SHORT).show();
			}else{
				notifyAdapter();
			}
			super.dispatchMessage(msg);
		}
	};
	
	public Tab3_MainView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}

	private void init() {
		//
		LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.tab3_main, this, true);
		//
		data = getApp().getGetMakeBeanData();
		mAdapter = new Tab3_Adapter(getContext(), data);
		mListView = (ListView) this.findViewById(R.id.tab3_listview);
		mListView.setDivider(new ColorDrawable(Color.TRANSPARENT));// 去除listview分隔線
		
		LinearLayout layout = new LinearLayout(getContext());
		ImageView footer2 = new ImageView(getContext());
		footer2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		footer2.setImageResource(R.drawable.footer_img);
		layout.addView(footer2);
		mListView.addFooterView(layout);
		mListView.setAdapter(mAdapter);
		//短按
		mListView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				//避免點到footer
				if (data.size() > 0) {
					if (arg2 < data.size()) {
						getApp().fadein = true;
						Message msg = Tab3_MainView.this.getApp().mChangeViewHandler.obtainMessage();
						msg.what = Tab3_MainView.this.getApp().MSG_WHAT_CHANGEVIEW;
						CourseSingleBundleBean bundleBean = new CourseSingleBundleBean( data.get(arg2).getContentId(), data.get(arg2).getContentName());
						ViewChageDataBean dataBean = new ViewChageDataBean(Tab3_MainActivity.class, Tab1_CourseSingle.class, bundleBean);
						msg.obj = dataBean;
						Tab3_MainView.this.getApp().mChangeViewHandler.sendMessage(msg);
						Tab3_MainView.this.getApp().tab3InCourseSingle = true;
						Tab3_MainView.this.getApp().tab2InCourseSingle = false;
					}
				}
			}
		});
		//長按
		mListView.setOnItemLongClickListener(new OnItemLongClickListener() {

			public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) {
				//避免點到footer
				if (arg2 < data.size()) {
					String[] title = { "取消","確定"};
					Helper.ShowMessage((Activity) getContext(), "", "是否確定清除過期資料", 2, title, null, null, new DialogInterface.OnClickListener() {
						
						public void onClick(DialogInterface dialog, int which) {

							HideMakeExpireEvent hideMakeExpireEvent = new HideMakeExpireEvent(null, data.get(arg2).getMakeId());
							hideMakeExpireEvent.addCallback(new IEventCallback() {
								
								public void result(Event event, Object result) {
									if (((HideMakeExpireBean)result).getResult() == 0) {
										data.remove(arg2);
										handler.sendEmptyMessage(0);
									}else{
										Message msg = handler.obtainMessage(0, ((HideMakeExpireBean)result).getMessage());
										handler.sendMessage(msg);
									}
								}
								public void progress(Event event, int progress, String tmpPath) {
								}
							});
							KenEventManager.getInstance().execute(hideMakeExpireEvent);
						}
					});
				}
				return true;
			}
		});
		//清除所有過期資料
		mTab3Button = (ImageView) this.findViewById(R.id.tab3_button);
		mTab3Button.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String[] title = { "取消","確定"};
				Helper.ShowMessage((Activity) getContext(), "", "是否確定清除所有過期資料", 2, title, null, null, new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						HideMakeExpireEvent hideMakeExpireEvent = new HideMakeExpireEvent(null ,0);
						hideMakeExpireEvent.addCallback(new IEventCallback() {
							
							public void result(Event event, Object result) {
								if (((HideMakeExpireBean)result).getResult() == 0) {
									GetMakeEvent getMakeEvent = new GetMakeEvent();
									getMakeEvent.addCallback(new IEventCallback() {
										
										public void result(Event event, Object result) {
											getApp().setGetMakeBeanData((List<GetMakeBean>) result);
											handler.sendEmptyMessage(0);
										}
										public void progress(Event event, int progress, String tmpPath) {
										}
									});
									KenEventManager.getInstance().execute(getMakeEvent);
									Message msg = handler.obtainMessage(0, ((HideMakeExpireBean)result).getMessage());
									handler.sendMessage(msg);
								}else{
									Message msg = handler.obtainMessage(0, ((HideMakeExpireBean)result).getMessage());
									handler.sendMessage(msg);
								}
							}
							public void progress(Event event, int progress, String tmpPath) {
							}
						});
						KenEventManager.getInstance().execute(hideMakeExpireEvent);
					}
				});
				
			}
		});
		
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub

	}
	
	public void notifyAdapter(){
		mAdapter.notifyDataSetChanged();
	}

}
